package com.example.flowabledemo.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.api.Task;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName: TaskCreateListener
 * @Description:
 * @Date 2024/05/21 10:31
 * @Author songshaoqiu
 **/
@Component
@Slf4j
public class TaskCreateListener implements TaskListener, ApplicationContextAware {

     private static RepositoryService repositoryService;

     private  static TaskService taskService;

    private   ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        RepositoryService repositoryService1 = applicationContext.getBean(RepositoryService.class);
        TaskCreateListener.repositoryService = repositoryService1;
        TaskService taskService1 = applicationContext.getBean(TaskService.class);
        this.taskService = taskService1;
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("create_事件执行了,【{}】",delegateTask);
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        String processInstanceId = delegateTask.getProcessInstanceId();

        Task task = taskService.createTaskQuery().taskDefinitionKey(taskDefinitionKey)
                .processInstanceId(processInstanceId)
                .singleResult();

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext =applicationContext;
    }
}
